﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.RM.Bll;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Other
{
    public partial class FrmPicking1 : Form
    {
        public FrmPicking1()
        {
            InitializeComponent();
        }

        DataTable DtRMOut;

        string _BillNo = "";
        int _RowNo = 0;

        string _PartNumber = string.Empty;  //料号
        string _DataCode = string.Empty;

        public void clearall()
        {
            DtRMOut.Clear();
            _RowNo = 0;
            _PartNumber = string.Empty;  //料号
            _DataCode = string.Empty;
        }

        private void FrmRMOut_Load(object sender, EventArgs e)
        {
            InitIvt();
            txtBillNo.Focus();
        }

        public void GetBillInfo()
        {
            try
            {
                lblLocation.Text = "";
                lab_msg.Text = "";

                var txtbillnoList = txtBillNo.Text.Split('-');
                if (txtbillnoList.Length >= 2)
                {
                    _BillNo = txtbillnoList[0];
                    _RowNo = Convert.ToInt32(txtbillnoList[1]);
                }
                else
                {
                    MessageBox.Show("请选择正确的单号!");
                    txtBillNo.Text = "";
                    return;
                }
                DtRMOut = Bll_RMOut.GetOutRow(_BillNo, _RowNo);

                decimal realQty = 0;
                decimal planQty = 0;
                var storageSN = "";
                var pickQty = "0";
                if (DtRMOut != null && DtRMOut.Rows.Count > 0)
                {
                    _PartNumber = DtRMOut.Rows[0]["PartNumber"].ToString();
                    realQty = Convert.ToDecimal(DtRMOut.Rows[0]["RealQty"]);
                    planQty = Convert.ToDecimal(DtRMOut.Rows[0]["PlanQty"]);
                    storageSN = DtRMOut.Rows[0]["Storage_SN"].ToString();

                    //拣货区数量
                    string sqlPick = "SELECT * FROM wms_T_Inventory WHERE Area_SN = '{0}' AND PartNumber='{1}'";
                    sqlPick = string.Format(sqlPick, storageSN + "#Pick", _PartNumber);
                    var dtPick = Utils.NMS.GetDataTable(sqlPick);
                    if (dtPick.Rows.Count > 0)
                    {
                        pickQty = dtPick.Rows[0]["Qty"].ToString();
                    }
                }
                else
                {
                    MessageBox.Show("请选择正确的单号!");
                    txtBillNo.Text = "";
                    return;
                }


                lab_msg.Text = "单号:" + _BillNo + ",行号:" + _RowNo +
                    "\r\n料号:" + _PartNumber.Trim() + "\r\n计划数量:" + planQty + ",版本:" + DtRMOut.Rows[0]["Ext3"].ToString();
                lab_msg.Text += "\r\n拣货区数量:" + pickQty;

                var dt = Bll_RMOut.GetRecommend(storageSN, _PartNumber, DtRMOut.Rows[0]["Ext3"].ToString());
                if (dt != null && dt.Rows.Count > 0)
                {
                    dgRecommend.DataSource = dt;
                }
                else
                {
                    var qty = Bll_RMOut.GetReceiptAreaQty(_PartNumber);
                    MessageBox.Show("库存不足,待上架库存:" + qty.ToString());
                }
                dgRecommend.Focus();
            }
            catch
            {
                txtBillNo.Text = "";
                txtBillNo.Focus();
            }
        }

        private void txtBillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                GetBillInfo();
            }
        }

        private void BtnOver_Click(object sender, EventArgs e)
        {
            txtBillNo.Focus();
            txtBillNo.Text = "";
            _BillNo = "";
            _RowNo = 0;
            clearall();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            GetBillInfo();
        }

        private void InitIvt()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dts.MappingName = "Result";

            this.dgRecommend.TableStyles.Add(dts);
            this.dgRecommend.TableStyles[0].GridColumnStyles.Clear();

            DataGridTextBoxColumn colPt = new DataGridTextBoxColumn();
            colPt.Width = 120;
            colPt.HeaderText = "库位";
            colPt.MappingName = "Location_SN";
            this.dgRecommend.TableStyles[0].GridColumnStyles.Add(colPt);

            DataGridTextBoxColumn colQty = new DataGridTextBoxColumn();
            colQty.Width = 76;
            colQty.HeaderText = "数量";
            colQty.MappingName = "Qty";
            this.dgRecommend.TableStyles[0].GridColumnStyles.Add(colQty);
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            var locationSN = this.lblLocation.Text;
            if (string.IsNullOrEmpty(locationSN))
            {
                MessageBox.Show("请先选择库位", "消息提示");
                return;
            }
            FrmPicking2 frm = new FrmPicking2(_BillNo, locationSN, _PartNumber);
            frm.ShowDialog();
            GetBillInfo();
        }

        private void dgRecommend_CurrentCellChanged(object sender, EventArgs e)
        {
            var rowIndex = dgRecommend.CurrentRowIndex;
            this.lblLocation.Text = dgRecommend[rowIndex, 0].ToString();
        }

        private void dgRecommend_Click(object sender, EventArgs e)
        {
            var rowIndex = dgRecommend.CurrentRowIndex;
            this.lblLocation.Text = dgRecommend[rowIndex, 0].ToString();
        }

        private void dgRecommend_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                var locationSN = this.lblLocation.Text;
                if (string.IsNullOrEmpty(locationSN))
                {
                    MessageBox.Show("请先选择库位", "消息提示");
                    return;
                }
                FrmPicking2 frm = new FrmPicking2(_BillNo, locationSN, _PartNumber);
                frm.ShowDialog();
                GetBillInfo();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtBillNo.Text = "";
            GetBillInfo();
            this.dgRecommend.DataSource = null;
        }

        //END
    }
}